package com.liu.controller;

import com.liu.domain.rule;
import com.liu.domain.student;
import com.liu.repository.JiFenRepository;
import com.liu.repository.RuleRepository;
import com.liu.utils.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 17-10-15.
 */
@RestController
public class RuleController {
    private final static Logger logger = LoggerFactory.getLogger(RuleController.class);

    @Autowired
    private RuleRepository ruleRepository;

    @Autowired
    JiFenRepository jiFenRepository;

    /*
    查询所有列表
     */
    @GetMapping(value = "/rule")
    public String ruleList(String rid,String sj) throws ParseException {
        System.out.println(sj);
        System.out.println(rid);

        if (sj != null && sj != "") {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date date = sdf.parse(sj);
            System.out.println(date);
            List<rule> rules = ruleRepository.findbytime(date);
            for (rule r : rules) {
                int rule = r.getR_id();
                String r_id = String.valueOf(rule);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoomId(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + rules.size() + ",\"data\":" + rules + "}";
        }else if (rid !=null && rid != "") {
            Integer i;
            i = Integer.valueOf(rid);
            List<rule> ruleList = ruleRepository.findbyroom(i);
            for (rule r : ruleList) {
                int rule = r.getR_id();
                String r_id = String.valueOf(rule);
                String buildId = r_id.substring(0, 2);
                r_id = r_id.substring(2, 5);
                r.setRoomId(buildId + "#" + r_id);
            }
            return "{\"code\":0,\"msg\":\"\",\"count\":" + ruleList.size() + ",\"data\":" + ruleList + "}";

        }
        List<rule> rules = ruleRepository.findAll();
        for (rule r: rules) {
            int room = r.getR_id();
            String roomId = String.valueOf(room);
            String buildId = roomId.substring(0, 2);
            roomId = roomId.substring(2, 5);
            r.setRoomId(buildId + "#" + roomId);
        }

        //return ruleRepository.findAll();
        return "{\"code\":0,\"msg\":\"\",\"count\":" + rules.size() + ",\"data\":" + ruleRepository.findAll() + "}";

    }

    /*
    查询最新的前两条数据
     */
    @GetMapping(value = "/ruleda")
    public List<rule> ruleList1(){
        List<rule> rules = ruleRepository.findbydate();
        for (rule r: rules) {
            int room = r.getR_id();
            String roomId = String.valueOf(room);
            String buildId = roomId.substring(0, 2);
            roomId = roomId.substring(2, 5);
            r.setRoomId(buildId + "#" + roomId);
        }
        List nList=rules.subList(0, 3);
        System.out.print(nList);

        return nList;

    }
    /*
    添加一个寝室
     */
    @PostMapping(value = "/rule")
    public ModelAndView testAdd(rule ru,int R_id) {
        ruleRepository.save(ru);
       /* System.out.print("寝室号:" +R_id);*/
        jiFenRepository.modifyRoomScoreRule(R_id);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("addRule");
        mav.addObject("flag", 1);
        return mav;
    }

    /*
    更新一个寝室的信息
     */
    @PutMapping(value = "/rule/{id}")
    public rule studentUpdate(@PathVariable("id") int id, rule ru) {

        return ruleRepository.save(ru);
    }
    /*
    删除一个寝室
     */

    @DeleteMapping(value = "/rule/{id}")
    public void studentDelete(@PathVariable("id") int id){
        ruleRepository.delete(id);
    }

    //学生主页面 本寝学生违规情况
    @RequestMapping("/getRoomRule")
    public List<rule> getClassStu(HttpSession session, String type) {
        student stu = (student) session.getAttribute("user");
        List<rule> rules= ruleRepository.findbyroom(stu.getRoom_id());
        return  rules;
    }


}
